package com.zwps.biz.domain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwps.biz.api.model.vo.fourpipes.TrainNewVO;
import com.zwps.biz.api.model.vo.fourpipes.TrainPlanVO;
import com.zwps.biz.api.model.vo.fourpipes.TrainRecordVO;
import com.zwps.biz.dal.db.dao.ZTrainPlanDAO;
import com.zwps.biz.dal.db.dao.ZTrainRecordDAO;
import com.zwps.biz.dal.db.object.ZTrainPlanDO;
import com.zwps.biz.dal.db.object.ZTrainRecordDO;
import com.zwps.biz.domain.service.ZTrainRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author Administrator
 */
@DS("lhk")
@Service
public class ZTrainRecordServiceImpl extends ServiceImpl<ZTrainRecordDAO, ZTrainRecordDO>
    implements ZTrainRecordService{
    @Autowired
    private ZTrainRecordDAO zTrainRecordDAO;
    @Autowired
     private ZTrainPlanDAO zTrainPlanDAO;
    @Override
    public TrainNewVO getTrainRecordByType(String year, String type) {
        TrainNewVO trainNewVO = new TrainNewVO();

        LambdaQueryWrapper<ZTrainPlanDO> planQueryWrapper = new LambdaQueryWrapper<>();
        if (year != null) {
            planQueryWrapper.eq(ZTrainPlanDO :: getPlanTime, year);
        }
        if (type != null) {
            planQueryWrapper.eq(ZTrainPlanDO::getType, type);
        }
        planQueryWrapper.eq(ZTrainPlanDO::getIsDelete,0);

        int planCount = Math.toIntExact(zTrainPlanDAO.selectCount(planQueryWrapper));
        trainNewVO.setPalnCount(planCount);
        //查询计划列表
        List<ZTrainPlanDO> zTrainPlanDOS = zTrainPlanDAO.selectList(planQueryWrapper);
        List<TrainPlanVO> trainPlanVOList = zTrainPlanDOS.stream()
                .map(ZTrainPlanDO -> BeanUtil.copyProperties(ZTrainPlanDO, TrainPlanVO.class))
                .collect(Collectors.toList());
        trainNewVO.setTrainPlanVOList(trainPlanVOList);
        LambdaQueryWrapper<ZTrainRecordDO> recordQueryWrapper = new LambdaQueryWrapper<>();
        if (year != null) {
            recordQueryWrapper.apply("YEAR(train_time) = {0}", year);
        }
        if (type != null) {
            recordQueryWrapper.eq(ZTrainRecordDO::getType, type);
        }
        recordQueryWrapper.eq(ZTrainRecordDO::getIsDelete,0);

        // 统计记录数量
        int recordCount = Math.toIntExact(zTrainRecordDAO.selectCount(recordQueryWrapper));
        trainNewVO.setRecordCount(recordCount);

        // 查询详细记录
        List<ZTrainRecordDO> zTrainRecordDOS = zTrainRecordDAO.selectList(recordQueryWrapper);
        List<TrainRecordVO> trainDetailVOList = zTrainRecordDOS.stream()
                .map(zTrainRecordDO -> BeanUtil.copyProperties(zTrainRecordDO, TrainRecordVO.class))
                .collect(Collectors.toList());
        trainNewVO.setTrainDetailVOList(trainDetailVOList);
            if ("1".equals(type)){
                trainNewVO.setPalnCount(trainNewVO.getRecordCount());
            }
        return trainNewVO;

    }
}




